package cn.team11.smarteye.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import cn.team11.sds.dayi.R;
import com.eques.icvss.utils.ResultCode;

import org.apache.commons.lang3.StringUtils;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.team11.smarteye.base.BaseActivity;
import cn.team11.smarteye.base.BaseFragment;
import cn.team11.smarteye.base.BasePresenter;
import cn.team11.smarteye.bean.CateyeEvent;
import cn.team11.smarteye.contract.DeviceBindingContract;
import cn.team11.smarteye.fragment.ConfigNetworkFragment;
import cn.team11.smarteye.fragment.DeviceBindResultFragment;
import cn.team11.smarteye.impl.DeviceBindingPresenterImpl;
import cn.team11.smarteye.misc.SConstants;
import cn.team11.smarteye.utils.PreferencesUtils;
import cn.team11.smarteye.utils.ToastUtil;
import cn.team11.smarteye.widget.CustomToolbar;

/**
 * Created by Yang on 2017/10/29.
 */

public class DeviceConfigActivity extends BaseActivity<DeviceBindingContract.Presenter> implements BaseFragment.FragmentListener, DeviceBindingContract.View{

    @BindView(R.id.bind_title_toolbar)
    CustomToolbar mToolbar;

    private String username;
    private String keyid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_device);
        ButterKnife.bind(this);
        init();
    }

    @Override
    public void init(){
        getSupportFragmentManager().beginTransaction()
                .add(R.id.bind_fragment_content, ConfigNetworkFragment.newInstance(null))
                .addToBackStack(ConfigNetworkFragment.class.getSimpleName())
                .commitAllowingStateLoss();
        mToolbar.setBackAction(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                backPressAction();
            }
        });

        username = PreferencesUtils.getString(SConstants.USERNAME);
        keyid = PreferencesUtils.getString(SConstants.KEYID);

        new DeviceBindingPresenterImpl(this);
    }

    @Override
    public void onLoginResult(boolean result) {
        if (result){
            //登录成功
        } else {
            //登录失败
        }
    }

    @Override
    public void onNetAvailable(boolean isAvailable) {

    }

    @Override
    public void onCateyeServiceBinded() {

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onBackPressed() {
        backPressAction();
    }

    public void backPressAction() {
        final int count = getSupportFragmentManager().getBackStackEntryCount();
        if (count>1){
            getSupportFragmentManager().popBackStack();
        } else {
            finish();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            overridePendingTransition(R.anim.slide_in_left_without_interpolator, R.anim.slide_out_right_without_interpolator);
        }
    }

    @Override
    public void setTitle(String title) {
        mToolbar.setMidTitle(title);
    }



    @Override
    public void setPresenter(DeviceBindingContract.Presenter presenter) {
        this.mBasePresenter = presenter;
    }

    @Override
    public Context getContext() {
        return getApplicationContext();
    }



    /** 获取当前wifi名称*/
    public String getCurrentNetworkName(){
        if (mBasePresenter != null){
            return mBasePresenter.getNetworkName();
        }
        return "";
    }


    /**生成二维码*/
    public Bitmap createQrcodeImage(final String ssid, final String pwd){
        if (mBasePresenter != null){
            return  mBasePresenter.createQrCodeImage(ssid, pwd, keyid, username);
        } else
            return null;
    }


     @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBindRequestResult(CateyeEvent.RequestBinding result) {
           if (!StringUtils.isEmpty(result.oldbdy)){
               showToast(ToastUtil.NEGATIVE_TOAST, "该设备已经被"+result.oldbdy+"绑定!");
           } else if (StringUtils.isEmpty(result.reqid)){
               showToast(ToastUtil.NEGATIVE_TOAST, "建立会话出错!");
           } else {
               if (mBasePresenter != null){
                   mBasePresenter.ackAddDeviceResponse(result.reqid, 1);
               }
           }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBindResult(CateyeEvent.ResultBinding result) {
        if (result.code == ResultCode.SUCCESS){
            //打开结果fragment
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.bind_fragment_content, DeviceBindResultFragment.newInstance(null))
                    .addToBackStack(DeviceBindResultFragment.class.getSimpleName())
                    .commitAllowingStateLoss();
        } else {
            showToast(ToastUtil.NEGATIVE_TOAST, "添加设备失败");
            onBackPressed();
        }
    }

}
